#include "draw.h"

/** @fn     void drawFrame(int width, int height)
    @brief  draws a frame given a width and height
    @param  width
    @param  height
    @return void
    */
int drawFrame(int top, int width, int height, int collumns){
    int i, j, k;
    width--;
    height = (height+top)-1;

    SetColor(FRAME, FRAMEBG);
    //draw horizontal lines
    for(i = 1; i<width; i++){
        //top
        PlaceCursor(i, top);
        printf("%c", 205);
        //bottom
        PlaceCursor(i, height);
        printf("%c", 205);
    }

    //draw sidelines
    for(i = top+1; i<height; i++){
        //left
        PlaceCursor(0, i);
        printf("%c", 186);
        //right
        PlaceCursor(width, i);
        printf("%c", 186);
    }

    //draw corners
    //top left
    PlaceCursor(0,top);
    printf("%c", 201);
    //top right
    PlaceCursor(width,top);
    printf("%c", 187);
    //bottom left
    PlaceCursor(0,height);
    printf("%c", 200);
    //bottom right
    PlaceCursor(width,height);
    printf("%c", 188);

    if(collumns <= 1){return(0);}

    //deal with collumns
    int collumnoffset = width/collumns;
    //for each collumn
    for(i = 1; i < collumns; i++){
        j = collumnoffset*i;
        //print top "T" connector
        PlaceCursor(j, top);
        printf("%c", 203);
        //print divider line on each row
        for(k = top+1; k<height; k++){
            PlaceCursor(j, k);
            printf("%c", 186);
        }
        //print bottom "T" connector
        PlaceCursor(j, height);
        printf("%c", 202);
    }

    return(1);
}

/** @fn     void drawTextBox(int x, int line, int width)
    @brief  draws a textbox
    @param  row     the row where to start drawing the textbox
    @param  line    the line at which to draw the textbox
    @param  width   the width of the textbox (in characters)
    @return void
    */
void drawTextBox(int row, int line, int width, char *str){
    int i, ctrl;

    width = width < gWIDTH-1-row ? width : gWIDTH-2-row;

    SetColor(TEXTBOX, TEXTBOXBG);
    ctrl = 0;
    for(i=row;i<width+row;i++){
        PlaceCursor(i, line);
        //set control bit if end of line is detected
        if(*(str+(i-row)) == '\0'){ctrl++;}
        //print chars if eol is not set
        if(!ctrl){
            printf("%c", *(str+(i-row)));
        }else{
            printf("%c", 255);
        }
    }
    PlaceCursor(row, line);
}

/** @fn     void drawBar(int row, char *str1, char *str2, int color, int bgcolor)
    @brief  draws a bar and populates it with the necessary text
    @param  row     the row at which to render the bar
    @param  str1    left string
    @param  str2    right string
    @param  color   the color of the text
    @param  color   the background color of the bar
    @return void
    */
void drawBar(int row, char *str1, char *str2, int color, int bgcolor){
    SetColor(color, bgcolor);

    int i = 0;
    //draw bar
    for(i = 0; i < gWIDTH; i++){
        PlaceCursor(i, row);
        printf("%c", 255);
    }

    //insert text
    i = 0;
    //print first term from the beginning of the bar
    PlaceCursor(0, row);
    while(*(str1+i) != '\0'){
        printf("%c", *(str1+i));
        i++;
    }

    i = 0;
    //print second term
    while(*(str2+i) != '\0'){
        //check length of second term
        i++;
    }

    PlaceCursor(gWIDTH-i, row);
    i = 0;
    while(*(str2+i) != '\0'){
        //check length of second term
        printf("%c", *(str2+i));
        i++;
    }

    //set default colors on drawbar exit
    SetColor(DEFAULT, DEFAULTBG);
}
